package com.yugong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugong.common.Const;
import com.yugong.entity.dto.*;
import com.yugong.entity.vo.request.OrderClassAdminRequestVO;
import com.yugong.entity.vo.request.StudentRequestVO;
import com.yugong.entity.vo.response.CourseScoreListResponseVO;
import com.yugong.entity.vo.response.OrderClassAdminListResponseVO;
import com.yugong.entity.vo.response.StudentListResponseVO;
import com.yugong.mapper.*;
import com.yugong.service.OrderClassAdminService;
import com.yugong.util.DigestUtils;
import com.yugong.util.TimeUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单班管理员业务逻辑实现类
 *
 * @author yugong
 * @date 2024/2/18
 */
@Service
public class OrderClassAdminServiceImpl extends ServiceImpl<OrderClassAdminMapper, OrderClassAdmin> implements OrderClassAdminService {
    @Resource
    private OrderClassAdminMapper orderClassAdminMapper;
    @Resource
    private OrderClassMapper orderClassMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CollageMapper collageMapper;
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private CourseScoreMapper courseScoreMapper;
    @Resource
    private TimeUtils timeUtils;
    @Resource
    private DigestUtils digestUtils;

    /**
     * 根据姓名查询订单班管理员
     *
     * @param pageNum             当前页
     * @param pageSize            当前页数据总个数
     * @param orderClassAdminName 姓名
     * @return 订单班管理员
     */
    @Override
    public PageInfo<OrderClassAdminListResponseVO> findOrderClassAdmin(Integer pageNum, Integer pageSize, String orderClassAdminName) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<OrderClassAdmin> orderClassAdminLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderClassAdminLambdaQueryWrapper
                .like(OrderClassAdmin::getOrderClassAdminName, orderClassAdminName)
                .orderByDesc(OrderClassAdmin::getOrderClassAdminCreateTime);
        List<OrderClassAdmin> orderClassAdminList = orderClassAdminMapper.selectList(orderClassAdminLambdaQueryWrapper);
        List<OrderClassAdminListResponseVO> orderClassAdminListResponseVOList = new ArrayList<>();
        QueryWrapper<OrderClassAdmin> orderClassAdminQueryWrapper = new QueryWrapper<>();
        orderClassAdminQueryWrapper.like("order_class_admin_name", orderClassAdminName);
        Long orderClassAdminCountNumber = orderClassAdminMapper.selectCount(orderClassAdminQueryWrapper);
        for (OrderClassAdmin orderClassAdmin : orderClassAdminList) {
            OrderClassAdminListResponseVO orderClassAdminListResponseVO = new OrderClassAdminListResponseVO();
            orderClassAdminListResponseVO.setOrderClassAdminNo(orderClassAdmin.getOrderClassAdminNo());
            if ("".equals(orderClassAdmin.getOrderClassAdminName())) {
                orderClassAdminListResponseVO.setOrderClassAdminName("未填写");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminName(orderClassAdmin.getOrderClassAdminName());
            }
            orderClassAdminListResponseVO.setOrderClassAdminAge(orderClassAdmin.getOrderClassAdminAge());
            if (1 == orderClassAdmin.getOrderClassAdminSex()) {
                orderClassAdminListResponseVO.setOrderClassAdminSex("男");
            } else if (0 == orderClassAdmin.getOrderClassAdminSex()) {
                orderClassAdminListResponseVO.setOrderClassAdminSex("女");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminSex("未填写");
            }
            Room room = roomMapper.selectById(orderClassAdmin.getOrderClassAdminRoomNo());
            if (room == null) {
                orderClassAdminListResponseVO.setOrderClassAdminRoomNo("未填写");
                orderClassAdminListResponseVO.setOrderClassAdminRoomName("未填写");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminRoomNo(room.getRoomNo());
                orderClassAdminListResponseVO.setOrderClassAdminRoomName(room.getRoomName());
            }
            Collage collage = collageMapper.selectById(orderClassAdmin.getOrderClassAdminCollageNo());
            if (collage == null) {
                orderClassAdminListResponseVO.setOrderClassAdminCollageNo("未填写");
                orderClassAdminListResponseVO.setOrderClassAdminCollageName("未填写");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminCollageNo(collage.getCollageNo());
                orderClassAdminListResponseVO.setOrderClassAdminCollageName(collage.getCollageName());
            }
            Account account = accountMapper.selectById(orderClassAdmin.getOrderClassAdminNo());
            if ("".equals(account.getAccountPhone())) {
                orderClassAdminListResponseVO.setOrderClassAdminPhone("未填写");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminPhone(digestUtils.base64Decode(account.getAccountPhone()));
            }
            if ("".equals(account.getAccountEmail())) {
                orderClassAdminListResponseVO.setOrderClassAdminEmail("未填写");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminEmail(digestUtils.base64Decode(account.getAccountEmail()));
            }
            orderClassAdminListResponseVO.setOrderClassAdminCreateMan(orderClassAdmin.getOrderClassAdminCreateMan());
            String createTime = timeUtils.setDateToStrDate(orderClassAdmin.getOrderClassAdminCreateTime(), Const.YYYY_MM_DD);
            orderClassAdminListResponseVO.setOrderClassAdminCreateTime(createTime);
            if (orderClassAdmin.getOrderClassAdminUpdateMan() == null) {
                orderClassAdminListResponseVO.setOrderClassAdminUpdateMan("未更新");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminUpdateMan(orderClassAdmin.getOrderClassAdminUpdateMan());
            }
            if (orderClassAdmin.getOrderClassAdminUpdateTime() == null) {
                orderClassAdminListResponseVO.setOrderClassAdminUpdateTime(null);
            } else {
                String updateTime = timeUtils.setDateToStrDate(orderClassAdmin.getOrderClassAdminUpdateTime(), Const.YYYY_MM_DD);
                orderClassAdminListResponseVO.setOrderClassAdminUpdateTime(updateTime);
            }
            orderClassAdminListResponseVO.setOrderClassAdminCountNumber(orderClassAdminCountNumber);
            orderClassAdminListResponseVOList.add(orderClassAdminListResponseVO);
        }
        return PageInfo.of(orderClassAdminListResponseVOList);
    }

    /**
     * 删除订单班管理员
     *
     * @param orderClassAdminNo 工号
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteOrderClass(String orderClassAdminNo) {
        int deleteAccountFlag = accountMapper.deleteById(orderClassAdminNo);
        int deleteOrderClassAdminFlag = orderClassAdminMapper.deleteById(orderClassAdminNo);
        return deleteAccountFlag + deleteOrderClassAdminFlag;
    }

    /**
     * 查询订单班管理员个人信息
     *
     * @param orderClassAdminNo 工号
     * @return 订单班管理员个人信息
     */
    @Override
    public OrderClassAdminListResponseVO orderClassAdminInfo(String orderClassAdminNo) {
        OrderClassAdmin orderClassAdmin = orderClassAdminMapper.selectById(orderClassAdminNo);
        OrderClassAdminListResponseVO orderClassAdminListResponseVO = new OrderClassAdminListResponseVO();
        orderClassAdminListResponseVO.setOrderClassAdminNo(orderClassAdminNo);
        if (orderClassAdmin == null) {

        } else {
            orderClassAdminListResponseVO.setOrderClassAdminName(orderClassAdmin.getOrderClassAdminName());
            if (0 == orderClassAdmin.getOrderClassAdminAge()) {
                orderClassAdminListResponseVO.setOrderClassAdminAge(null);
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminAge(orderClassAdmin.getOrderClassAdminAge());
            }
            if (1 == orderClassAdmin.getOrderClassAdminSex()) {
                orderClassAdminListResponseVO.setOrderClassAdminSex("男");
            } else if (0 == orderClassAdmin.getOrderClassAdminSex()) {
                orderClassAdminListResponseVO.setOrderClassAdminSex("男");
            } else {
                orderClassAdminListResponseVO.setOrderClassAdminSex("");
            }
            Room room = roomMapper.selectById(orderClassAdmin.getOrderClassAdminRoomNo());
            if (room == null) {
                orderClassAdminListResponseVO.setOrderClassAdminRoomNo("");
                orderClassAdminListResponseVO.setOrderClassAdminRoomName("");
            } else {
                if (room.getRoomNo() == null) {
                    orderClassAdminListResponseVO.setOrderClassAdminRoomNo("");
                } else {
                    orderClassAdminListResponseVO.setOrderClassAdminRoomNo(room.getRoomNo());
                }
                if (room.getRoomName() == null) {
                    orderClassAdminListResponseVO.setOrderClassAdminRoomName("");
                } else {
                    orderClassAdminListResponseVO.setOrderClassAdminRoomName(room.getRoomName());
                }
            }
            Collage collage = collageMapper.selectById(orderClassAdmin.getOrderClassAdminCollageNo());
            if (collage == null) {
                orderClassAdminListResponseVO.setOrderClassAdminCollageNo("");
                orderClassAdminListResponseVO.setOrderClassAdminCollageName("");
            } else {
                if (collage.getCollageNo() == null) {
                    orderClassAdminListResponseVO.setOrderClassAdminCollageNo("");
                } else {
                    orderClassAdminListResponseVO.setOrderClassAdminCollageNo(collage.getCollageNo());
                }
                if (collage.getCollageName() == null) {
                    orderClassAdminListResponseVO.setOrderClassAdminCollageName("");
                } else {
                    orderClassAdminListResponseVO.setOrderClassAdminCollageName(collage.getCollageName());
                }
            }
            Account account = accountMapper.selectById(orderClassAdminNo);
            if (account == null) {
                orderClassAdminListResponseVO.setOrderClassAdminPhone("");
                orderClassAdminListResponseVO.setOrderClassAdminEmail("");
            } else {
                if (account.getAccountPhone() == null) {
                    orderClassAdminListResponseVO.setOrderClassAdminPhone("");
                } else {
                    orderClassAdminListResponseVO.setOrderClassAdminPhone(digestUtils.base64Decode(account.getAccountPhone()));
                }
                if (account.getAccountEmail() == null) {
                    orderClassAdminListResponseVO.setOrderClassAdminEmail("");
                } else {
                    orderClassAdminListResponseVO.setOrderClassAdminEmail(digestUtils.base64Decode(account.getAccountEmail()));
                }
            }
        }
        return orderClassAdminListResponseVO;
    }

    /**
     * 修改个人信息
     *
     * @param orderClassAdminRequestVO 订单班管理员请求实体类
     * @return 数据库影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateInfo(OrderClassAdminRequestVO orderClassAdminRequestVO) {
        Date updateTime = new Date();
        Account account = new Account();
        account.setAccountNo(orderClassAdminRequestVO.getOrderClassAdminNo());
        account.setAccountPhone(digestUtils.base64Encode(orderClassAdminRequestVO.getOrderClassAdminPhone()));
        account.setAccountEmail(digestUtils.base64Encode(orderClassAdminRequestVO.getOrderClassAdminEmail()));
        account.setAccountUpdateMan(orderClassAdminRequestVO.getOrderClassAdminNo());
        account.setAccountUpdateTime(updateTime);
        int updateAccountFlag = accountMapper.updateById(account);
        OrderClassAdmin orderClassAdmin = new OrderClassAdmin();
        orderClassAdmin.setOrderClassAdminNo(orderClassAdminRequestVO.getOrderClassAdminNo());
        orderClassAdmin.setOrderClassAdminName(orderClassAdminRequestVO.getOrderClassAdminName());
        orderClassAdmin.setOrderClassAdminAge(orderClassAdminRequestVO.getOrderClassAdminAge());
        orderClassAdmin.setOrderClassAdminSex(orderClassAdminRequestVO.getOrderClassAdminSex());
        orderClassAdmin.setOrderClassAdminUpdateMan(orderClassAdminRequestVO.getOrderClassAdminNo());
        orderClassAdmin.setOrderClassAdminUpdateTime(updateTime);
        int updateOrderClassAdminFlag = orderClassAdminMapper.updateById(orderClassAdmin);
        return updateAccountFlag + updateOrderClassAdminFlag;
    }

    /**
     * 修改订单班管理员
     *
     * @param orderClassAdminRequestVO 订单班管理员请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int updateOrderClassAdmin(OrderClassAdminRequestVO orderClassAdminRequestVO) {
        OrderClassAdmin orderClassAdmin = new OrderClassAdmin();
        orderClassAdmin.setOrderClassAdminNo(orderClassAdminRequestVO.getOrderClassAdminNo());
        orderClassAdmin.setOrderClassAdminCollageNo(orderClassAdminRequestVO.getOrderClassAdminCollageNo());
        orderClassAdmin.setOrderClassAdminRoomNo(orderClassAdminRequestVO.getOrderClassAdminRoomNo());
        orderClassAdmin.setOrderClassAdminUpdateMan(orderClassAdminRequestVO.getOrderClassAdminUpdateMan());
        orderClassAdmin.setOrderClassAdminUpdateTime(new Date());
        return 0;
    }

    /**
     * 查看学生信息
     *
     * @param pageNum           当前页
     * @param pageSize          当前页总数
     * @param studentName       姓名
     * @param orderClassAdminNo 工号
     * @return 学生信息
     */
    @Override
    public PageInfo<StudentListResponseVO> findStudent(Integer pageNum, Integer pageSize, String studentName, String orderClassAdminNo) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<OrderClass> orderClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderClassLambdaQueryWrapper
                .eq(OrderClass::getOrderClassAdministratorNo, orderClassAdminNo);
        List<OrderClass> orderClassList = orderClassMapper.selectList(orderClassLambdaQueryWrapper);
        List<StudentListResponseVO> studentListResponseVOList = new ArrayList<>();
        Long studentCountNumber = 0L;
        for (OrderClass orderClass : orderClassList) {
            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            studentLambdaQueryWrapper
                    .eq(Student::getStudentOrderClassNo, orderClass.getOrderClassNo())
                    .like(Student::getStudentName, studentName);
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper
                    .eq("student_order_class_no", orderClass.getOrderClassNo())
                    .like("student_name", studentName);
            Long total = studentMapper.selectCount(studentQueryWrapper);
            studentCountNumber = studentCountNumber + total;
            List<Student> studentList = studentMapper.selectList(studentLambdaQueryWrapper);
            for (Student student : studentList) {
                StudentListResponseVO studentListResponseVO = new StudentListResponseVO();
                studentListResponseVO.setStudentNo(student.getStudentNo());
                studentListResponseVO.setStudentName(student.getStudentName());
                studentListResponseVO.setStudentOrderClassNo(student.getStudentOrderClassNo());
                studentListResponseVO.setStudentBusinessNo(student.getStudentBusinessNo());
                Business business = businessMapper.selectById(student.getStudentBusinessNo());
                OrderClass oc = orderClassMapper.selectById(student.getStudentOrderClassNo());
                if (business != null) {
                    if (business.getBusinessName() == null) {
                        studentListResponseVO.setStudentBusinessName("无");
                    } else {
                        studentListResponseVO.setStudentBusinessName(business.getBusinessName());
                    }
                } else {
                    studentListResponseVO.setStudentBusinessName("无");
                }
                if (oc.getOrderClassName() == null) {
                    studentListResponseVO.setStudentOrderClassName("无");
                } else {
                    studentListResponseVO.setStudentOrderClassName(oc.getOrderClassName());
                }
                if (student.getStudentEmploymentState() == 0) {
                    studentListResponseVO.setStudentEmploymentState("未录用");
                } else {
                    studentListResponseVO.setStudentEmploymentState("已录用");
                }
                studentListResponseVO.setStudentComprehensiveEvaluation(student.getStudentComprehensiveEvaluation());
                studentListResponseVO.setStudentCountNumber(studentCountNumber);
                studentListResponseVOList.add(studentListResponseVO);
            }
        }
        return PageInfo.of(studentListResponseVOList);
    }

    /**
     * 评价学生
     *
     * @param studentRequestVO 学生请求实体类
     * @return 数据库影响行数
     */
    @Override
    public int evaluateStudent(StudentRequestVO studentRequestVO) {
        Student student = new Student();
        student.setStudentNo(studentRequestVO.getStudentNo());
        student.setStudentComprehensiveEvaluation(studentRequestVO.getStudentComprehensiveEvaluation());
        student.setStudentUpdateMan(studentRequestVO.getStudentUpdateMan());
        student.setStudentUpdateTime(new Date());
        return studentMapper.updateById(student);
    }

    /**
     * 查看成绩
     *
     * @param pageNum           当前页
     * @param pageSize          当前页个数
     * @param studentName       姓名
     * @param orderClassAdminNo 工号
     * @return 成绩
     */
    @Override
    public PageInfo<CourseScoreListResponseVO> findScore(Integer pageNum, Integer pageSize, String studentName, String orderClassAdminNo) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<OrderClass> orderClassLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderClassLambdaQueryWrapper
                .eq(OrderClass::getOrderClassAdministratorNo, orderClassAdminNo);
        List<OrderClass> orderClassList = orderClassMapper.selectList(orderClassLambdaQueryWrapper);
        List<CourseScoreListResponseVO> courseScoreListResponseVOList = new ArrayList<>();
        for (OrderClass orderClass : orderClassList) {
            if (orderClass == null) {
                return null;
            } else {
                LambdaQueryWrapper<CourseScore> courseScoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
                courseScoreLambdaQueryWrapper
                        .eq(CourseScore::getCourseScoreOrderClassNo, orderClass.getOrderClassNo());
                QueryWrapper<CourseScore> courseScoreQueryWrapper = new QueryWrapper<>();
                courseScoreQueryWrapper
                        .eq("course_score_order_class_no", orderClass.getOrderClassNo());
                Long courseScoreCountNumber = courseScoreMapper.selectCount(courseScoreQueryWrapper);
                List<CourseScore> courseScoreList = courseScoreMapper.selectList(courseScoreLambdaQueryWrapper);
                for (CourseScore courseScore : courseScoreList) {
                    CourseScoreListResponseVO courseScoreListResponseVO = new CourseScoreListResponseVO();
                    courseScoreListResponseVO.setCourseScoreOrderClassNo(courseScore.getCourseScoreOrderClassNo());
                    courseScoreListResponseVO.setCourseScoreOrderClassName(orderClass.getOrderClassName());
                    Course course = courseMapper.selectById(courseScore.getCourseScoreCourseNo());
                    if (course == null) {
                        continue;
                    } else {
                        courseScoreListResponseVO.setCourseScoreCourseNo(courseScore.getCourseScoreCourseNo());
                        courseScoreListResponseVO.setCourseScoreCourseName(course.getCourseName());
                    }
                    LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    studentLambdaQueryWrapper
                            .eq(Student::getStudentNo, courseScore.getCourseScoreStudentNo())
                            .like(Student::getStudentName, studentName);
                    Student student = studentMapper.selectOne(studentLambdaQueryWrapper);
                    if (student == null) {
                        continue;
                    } else {
                        courseScoreListResponseVO.setCourseScoreStudentNo(courseScore.getCourseScoreStudentNo());
                        courseScoreListResponseVO.setCourseScoreStudentName(student.getStudentName());
                    }
                    courseScoreListResponseVO.setCourseScoreAchievement(courseScore.getCourseScoreAchievement());
                    courseScoreListResponseVO.setCourseScoreCountNumber(courseScoreCountNumber);
                    courseScoreListResponseVOList.add(courseScoreListResponseVO);
                }
            }
        }
        return PageInfo.of(courseScoreListResponseVOList);
    }
}
